<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Http\Requests\LocationLog\Store;
use App\Models\Device;
use App\Models\LocationLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class LocationLogController extends Controller
{
    protected $result = ['code' => 1, 'msg' => ''];
    const FINAL_CODE = 0;
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Store $request
     * @return \Illuminate\Http\Response
     */
    public function store(Store $request)
    {
        $validated = $request->validated();
        $validated = array_combine(
            [
                'device_id',
                'latitude',
                'longitude',
                'created_at',
                'address',
            ], $validated
        );
        $device = Device::query()->firstOrCreate(['device_id' => $validated['device_id']]);
        $validated['local_device_id'] = $device->id;
        $model = LocationLog::query()->create($validated);
        if ($model) {
            $this->result = ['code' => self::FINAL_CODE, 'msg' => '成功', 'data' => [$model]];
        } else {
//            Log::debug($model);
        }
        return response()->json($this->result);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
